# NuGet API 密钥
$apiKey = "oy2o4pejok2dwpvnjghal6dvbfcoehhuldxezcmww6shpy"

# NuGet �?
$nugetSource = "https://api.nuget.org/v3/index.json"

# 获取脚本所在的目录
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path

# 构建输出目录路径
$outputDirectory = Join-Path $scriptDirectory "Local_GuNet_Output"

# 输出信息
Write-Host "Script directory: $scriptDirectory"
Write-Host "Local_GuNet_Output directory: $outputDirectory"

# 检查并创建输出目录
if (-not (Test-Path -Path $outputDirectory)) {
    New-Item -Path $outputDirectory -ItemType Directory
    Write-Host "Local_GuNet_Output directory created."
}
# 清空本地版本
Remove-Item "$outputDirectory\*.nupkg" -Force

# 获取版本
$version = dotnet gitversion -showvariable NuGetVersionV2
Write-Host "当前版本号: $version"


# 查找解决方案目录下的所�?.csproj 文件
$csprojFiles = Get-ChildItem -Path $scriptDirectory -Filter "*.csproj" -Recurse

# 遍历每个 .csproj 文件，执行打包和发布操作
foreach ($csprojFile in $csprojFiles) {
    $csprojPath = $csprojFile.FullName
    $projectName = [System.IO.Path]::GetFileNameWithoutExtension($csprojFile.Name)
    
    # 打包
    Write-Host "Packing $projectName..."
    dotnet pack $csprojPath -c Release -o $outputDirectory /p:Version=$version
    
    # 发布
    $packagePath = Join-Path $outputDirectory "$projectName.*.nupkg"
    Write-Host "Publishing $packagePath..."
    dotnet nuget push $packagePath --source $nugetSource --api-key $apiKey
}

# 清空本地 NuGet 
Remove-Item "$outputDirectory\*.nupkg" -Force

Write-Host "All projects packed, published, and local packages cleared."
